package xkdaima;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;

/*
public class MCThreadTest
{
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        System.out.println("请设置维护开始时间与结束时间(格式：yyyy-MM-dd-HH-mm-ss)：");
        String starttime= input.next();
        String endtime= input.next();
        MCThread mcthread = new MCThread(starttime,endtime);

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        Calendar calendar = Calendar.getInstance();
        Date date = calendar.getTime();
        String systemtime = sdf.format(date);

        if(mcthread.judgedate(starttime)||mcthread.judgedate(endtime)){
            if(mcthread.comparedate(starttime,endtime)&&mcthread.comparedate(systemtime,endtime)){
                mcthread.start();
            }
            else{
                System.out.println("维护结束时间必须大于开始时间和系统时间！");
            }
        }
        else{
            System.out.println("维护开始时间或者结束时间格式不正确！");
        }
    }
}
*/
 public class MCThread extends Thread{//课程维护控制线程
    boolean mcopen=true;//课程维护开启，为false时不再重复修改
    boolean mcclose=false;//课程维护结束，为true时结束线程
    boolean comparetrue=true;//结束时间大于开始时间正确
    String starttime,endtime;

    public MCThread(String starttime,String endtime){
        super();
        this.starttime=starttime;
        this.endtime=endtime;
    }
    
    public MCThread() {
		
	}

    public void run() {
        while (true) {
            try {
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
                Calendar calendar = Calendar.getInstance();
                Date date = calendar.getTime();//获取系统时间
                String systemtime = sdf.format(date);
                CourseMaintain(systemtime,starttime,endtime);
                if(mcclose) break;
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void CourseMaintain(String systemtime, String starttime, String endtime){//课程维护(定时),系统时间（systemtime），开始时间（starttime），结束时间（endtime）
        try {
            Class.forName("org.sqlite.JDBC");
            Connection connection = DriverManager.getConnection("jdbc:sqlite:source/xuanke.sqlite");
            SimpleDateFormat sformat=new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
            Date startdate=new Date();
            startdate=sformat.parse(starttime);
            Date enddate=new Date();
            enddate=sformat.parse(endtime);
            Date systemdate=new Date();
            systemdate=sformat.parse(systemtime);
            if(((systemdate.compareTo(startdate))==0||(systemdate.compareTo(startdate))>0)&&mcopen)
            {
                PreparedStatement pst=connection.prepareStatement("update CTL set IfMCourse=1");
                pst.executeUpdate();
                System.out.println("课程维护开始!");
                pst.close();
                mcopen=false;
            }
            else if((systemdate.compareTo(enddate))==0||(systemdate.compareTo(enddate))>0){
                PreparedStatement pst=connection.prepareStatement("update CTL set IfMCourse=0");
                pst.executeUpdate();
                System.out.println("课程维护结束!");
                pst.close();
                mcclose=true;
            }
            connection .close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void CourseMaintainopen(){//开始课程维护(点击)
        try {
            Class.forName("org.sqlite.JDBC");
            Connection connection = DriverManager.getConnection("jdbc:sqlite:source/xuanke.sqlite");
            PreparedStatement pst=connection.prepareStatement("update CTL set IfMCourse=1");
            pst.executeUpdate();
            System.out.println("课程维护开始!");
            pst.close();
            connection .close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void CourseMaintainclose(){//结束课程维护(点击)
        try {
            Class.forName("org.sqlite.JDBC");
            Connection connection = DriverManager.getConnection("jdbc:sqlite:source/xuanke.sqlite");
            PreparedStatement pst=connection.prepareStatement("update CTL set IfMCourse=0");
            pst.executeUpdate();
            System.out.println("课程维护结束!");
            pst.close();
            connection .close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean comparedate(String starttime, String endtime){//比较日期大小(当starttime<endtime,return true;即结束时间大于开始时间正确)
        try {
            SimpleDateFormat sformat=new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
            Date startdate=new Date();
            startdate=sformat.parse(starttime);
            Date enddate=new Date();
            enddate=sformat.parse(endtime);
            if((enddate.compareTo(startdate))>0)
            {
                comparetrue=true;
            }
            else{
                comparetrue=false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return comparetrue;
    }

    public boolean judgedate(String str){//判断日期字符串格式是否正确(当格式正确,return true;)
        try {
            SimpleDateFormat sformat=new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
            sformat.setLenient(false);
            sformat.parse(str);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    
    public String getsystemtime() {
    	  SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
          Calendar calendar = Calendar.getInstance();
          Date date = calendar.getTime();
          String systemtime = sdf.format(date);
          return systemtime;
	}
}



